/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.hbrs.ooka.crs.servlet;

import de.hbrs.ooka.beans.UserBeanStatelessLocal;
import de.hbrs.ooka.beans.UserSessionStatefulLocal;
import de.hbrs.ooka.crs.servlet.constants.WebPageNames;
import de.hbrs.ooka.entities.UserEntity;
import java.io.IOException;
import javax.ejb.EJB;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class LoginServlet extends HttpServlet {
    
    @EJB
    private UserBeanStatelessLocal userBean;
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        UserSessionStatefulLocal crsBean = this.getCrsBean(request);
        UserEntity user = null;
        RequestDispatcher rd = null;
        
        String cmd = request.getParameter("action");
        String redirection = "";
                
        if (cmd == null) {
            if(crsBean.getUser() == null){
                redirection = WebPageNames.LoginViewPage + ".jsp";
            } else {
                redirection = WebPageNames.ParticipantViewPage;
            }
        } else {
            String name = request.getParameter("loginname");
            String password = request.getParameter("loginpassword");

            redirection = WebPageNames.LoginViewPage;
            if (cmd.equalsIgnoreCase("register")) {
                redirection = RegisterNewUser(name, password);

                request.setAttribute("message", "Registration Successful");
                rd = request.getRequestDispatcher("/" + WebPageNames.ActionSuccessfulPage);
                if(rd != null){
                    rd.forward(request, response);
                    return;
                }

            } else if (cmd.equalsIgnoreCase("login")) {
                user = LoginUser(name, password);
                if(user == null){
                    request.setAttribute("message", "User not found: " + name);
                    rd = request.getRequestDispatcher("/" + WebPageNames.ErrorPage + ".jsp");
                    if(rd != null){
                        rd.forward(request, response);
                        return;
                    }
                } else {
                    crsBean.setUser(user);
                    redirection = "ParticipantView";
                }                
            } else if (cmd.equalsIgnoreCase("logout")) {
                  this.removeCrsBean(request);
                  redirection = WebPageNames.LoginViewPage;
            }
        }
        response.sendRedirect(redirection);
    }
    
    private String RegisterNewUser(String name, String password) {
        UserEntity registrationOk = this.userBean.registerUser(name, password);
        return (registrationOk != null) ? WebPageNames.ActionSuccessfulPage : WebPageNames.LoginViewPage;
    }

    private UserEntity LoginUser(String name, String password) {
        UserEntity user = this.userBean.checkLogin(name, password);
        return user;
    }
    
    private UserSessionStatefulLocal getCrsBean(HttpServletRequest request) {
        HttpSession userSession = request.getSession(true);
        UserSessionStatefulLocal statefulBean = (UserSessionStatefulLocal) userSession.getAttribute("crsbean");
    
        if (statefulBean == null) {
          try {
              InitialContext ic = new InitialContext();
              statefulBean = (UserSessionStatefulLocal) ic.lookup("java:global/OOKA-CRS/OOKA-CRS-ejb/UserSessionStateful!de.hbrs.ooka.beans.UserSessionStatefulLocal");
              userSession.setAttribute("crsbean", statefulBean);
          } catch (NamingException e) {
              System.out.println(e.getMessage());
          }
        }
        return statefulBean;    
    }

    private void removeCrsBean(HttpServletRequest request) {
        HttpSession userSession = request.getSession(false);
        UserSessionStatefulLocal statefulBean = (UserSessionStatefulLocal) userSession.getAttribute("crsbean");
    
        if (statefulBean != null) {
          userSession.removeAttribute("crsbean");
        }
    }
}
